package com.exam.service.impl;

import com.exam.mapper.FillQuestionMapper;
import com.exam.mapper.JudgeQuestionMapper;
import com.exam.mapper.MultiQuestionMapper;
import com.exam.mapper.PaperManageMapper;
import com.exam.po.PaperManage;
import com.exam.service.QuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author mahaoqi
 * @ClassName FillQuestionServiceImpl
 * @Description 关于自动组卷的对题库进行一些操作的功能
 * @Date 2021/1/11 20:42
 * @Version 1.0
 **/
@Service
public class QuestionServiceImpl implements QuestionService {
    //随机生成题库
    private Random random = new Random();
    @Resource
    private FillQuestionMapper fillQuestionMapper;
    @Resource
    private JudgeQuestionMapper judgeQuestionMapper;
    @Resource
    private MultiQuestionMapper multiQuestionMapper;
    @Resource
    private PaperManageMapper paperManageMapper;

    /*
     * @Author mahaoqi
     * @Description 向中间表paper_manage进行插入操作
     * @Date 2021/1/13 10:29
     * @Param [paperid, fillIds, judgeIds, multiIds]
     * @return boolean
     **/
    public boolean addPaperManage(int paperid,Integer[] fillIds,Integer[] judgeIds,Integer[] multiIds){
        List<PaperManage> list = new ArrayList();
        for (Integer id:fillIds) {
            list.add(new PaperManage(paperid,2,id));
        }
        for (Integer id:judgeIds) {
            list.add(new PaperManage(paperid,3,id));
        }
        for (Integer id:multiIds) {
            list.add(new PaperManage(paperid,1,id));
        }
        return paperManageMapper.insertBatch(list)>0;
    }


    /*
     * @Author mahaoqi
     * @Description 以下三个方法一样,针对三个不同的题库表进行随机生成题目
     * @Date 2021/1/13 10:30
     * @Param [subject, num]
     * @return java.lang.Integer[]
     **/
    @Override
    public Integer[] findFillIds(String subject,int num) {
        int page=1;
        int pageSize=100;
        Integer[] findIds = fillQuestionMapper.findFillids(subject,page,pageSize);
        Integer[] fillIds = new Integer[num];
        for (int i=0;i<num;i++){
            fillIds[i] = findIds[random.nextInt(findIds.length)];
        }
        return fillIds;
    }
    @Override
    public Integer[] findJudgeIds(String subject,int num) {
        int page=1;
        int pageSize=100;

        Integer[] findIds = judgeQuestionMapper.findJudgeids(subject,page,pageSize);

        Integer[] judgeIds = new Integer[num];
        for (int i=0;i<num;i++){
            judgeIds[i] = findIds[random.nextInt(findIds.length)];
        }
        return judgeIds;
    }
    @Override
    public Integer[] findMultiIds(String subject,int num) {
        int page=1;
        int pageSize=100;
        Integer[] findIds = multiQuestionMapper.findMultiids(subject,page,pageSize);
        Integer[] multiIds = new Integer[num];
        for (int i=0;i<num;i++){
            multiIds[i] = findIds[random.nextInt(findIds.length)];
        }
        return multiIds;
    }
    /*
     * @Author mahaoqi
     * @Description 以上三个方法一样,针对三个不同的题库表进行随机生成题目
     * @Date 2021/1/13 10:30
     * @Param [subject, num]
     * @return java.lang.Integer[]
     **/


}
